package com.baosight.sczx.common.zm.cInterface.cb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.zm.cb.domain.ZMCBRoadway;

/**
 * 巷道 对外接口
 * <p>Title: RoadWay</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *         2013-7-3 上午11:01:49  高强 
 *</pre>
 */
public class RoadWay {

	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * <p>Function: getParnetInforWfBlock</p>
	 * <p>Description: </p>
	 * <p>return value: EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-06-21下午15:31:29  高强
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked"})
	public static EiBlock getParnetInfoRoadWayBlock()
	{
		EiBlock block = new EiBlock("cmes.cb.region");

		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("type");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map>  _alllist = new ArrayList<Map>();
		List<Map> mineList = dao.query("ZMCBMine.queryInterface", null);//矿井
		for(int i = 0; i < mineList.size(); i++) {
			_alllist.add(mineList.get(i));
		}
		
		List<Map>  levelList = dao.query("ZMCBLevel.queryInterface", null);//水平
		for(int i = 0; i < levelList.size(); i++) {
			_alllist.add(levelList.get(i));
		}
		
		List<Map>  mingAreaList = dao.query("ZMCBMiningarea.queryInterface", null);//采区
		for(int i = 0; i < mingAreaList.size(); i++) {
			_alllist.add(mingAreaList.get(i));
		}
		
		List<Map>  workingFaceList = dao.query("ZMCBWorkingface.queryInterface", null);//采面
		for(int i = 0; i < workingFaceList.size(); i++) {
			_alllist.add(workingFaceList.get(i));
		}
		
		block.addRows(_alllist);
		return block;
	}
	
	/**构造是或者否block
	 * <p>Function: getYesORNoBlock</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-06-21下午15:31:29  高强
	 * </p>
	 *
	*/
	@SuppressWarnings("rawtypes")
	public static EiBlock  getYesORNoBlock(String blockName)
	{
		EiBlock block = new EiBlock(blockName);

		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map>  _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		
        _map = new HashMap<String, String>();
		_map.put("display", "否");
		_map.put("value", "否");
        _list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("display", "是");
		_map.put("value", "是");
        _list.add(_map);
        
        block.setRows(_list);
		return block;
	}
	
	/**构造巷道名称block
	 * <p>Function: getYesORNoBlock</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-06-21下午15:31:29  梁琦
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked"})
	public static EiBlock getRoadWayBlock()
	{
		EiBlock block = new EiBlock("cmes.cb.RoadWayName");

		
		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdLength");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdRoadwayType");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdMineRock");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdSurplusLength");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdSection");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		block.addRows(getList());
		return block;
	}
	
	/**为树构造巷道节点
	 * <p>Function: getRoadWay</p>
	 * <p>Description: </p>
	 * <p>return value: RoadWay</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-07-17下午15:31:29  高强
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked"})
	public  static List getRoadWay (List<Map>  _list,Map<String, String> _argMap)
	{
		List<Map>  roadWayList = dao.query("ZMCBRoadway.queryTree", _argMap);//巷道
		for(int i = 0; i < roadWayList.size(); i++) {
			_list.add(roadWayList.get(i));
		}
		
		return _list;
	}
	
	/**
	 * 获取巷道(开采和设计)
	 * <p>Function: getList</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-8-28上午09:50:10 张志芳
	 * </p>
	 *
	 */
	public static List<Map> getList(){
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("notComplete", "yes");
		return dao.query("ZMCBRoadway.queryInterface", _map);//巷道
	}
	
	/**
	 * 判断某个采面下的巷道是否全部完工
	 * <p>Function: isComplete</p>
	 * <p>Description: </p>
	 * <p>return value:Boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-9-21上午06:53:26 张志芳
	 * </p>
	 *
	 */
	public static Boolean isComplete(String workfaceId){
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdParentId", workfaceId);
		List<Map> list = dao.query("ZMCBRoadway.queryInterface", _map);//巷道
		Boolean flag = true;
		for(int i=0;i<list.size();i++){
			if(!"完工".equals(list.get(i).get("fdStatus"))){
				flag = false;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 根据巷道ID获取其对应服务的采面
	 * <p>Function: getBelongWorkface</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-9-16上午07:10:29 张志芳
	 * </p>
	 *
	 */
	public static String getBelongWorkface(String roadwayId){
		String wf = "";
		Map map = new HashMap();
		map.put("fdKeyId", roadwayId);
		List<Map> list = dao.query("ZMCBRoadway.queryInterface", map);
		if(list.size()>0){
			String parent = (String)list.get(0).get("fdParentId");
			if(parent==null || "".equals(parent)) return wf;
			
			if("采面".equals(list.get(0).get("fdParentType"))){
				wf = parent;
			}
			else if("采区".equals(list.get(0).get("fdParentType"))){
				wf = Workingface.getFirstWorkface(parent);
			}
			else if("水平".equals(list.get(0).get("fdParentType"))){
				String area = Miningarea.getFirstMingingArea(parent);
				if(area!=null && !"".equals(area)){
					wf = Workingface.getFirstWorkface(area);
				}				
			}
			else if("矿井".equals(list.get(0).get("fdParentType"))){
				String level = Level.getFirstLevel(parent);
				if(level!=null && !"".equals(level)){
					String area = Miningarea.getFirstMingingArea(level);
					if(area!=null && !"".equals(area)){
						wf = Workingface.getFirstWorkface(area);
					}
				}
			}
		}
		return wf;
	}
}
